package c.x.jy.service.utils.mqtt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 

/**
 * Created by zdl on 2018/7/11.
 */
public class MQDeviceUtil {
    protected static Logger logger = LoggerFactory.getLogger(MQDeviceUtil.class);
   // private final static String BASE_URL = "https://www.58shareiot.com/iot-wechat-web/";
    private final static String DEVICE_STATUS_URL = "advertis/DevStatus";

    /**
     * 获取设备状态
     *
     * @param
     * @return
     */
 

   /*
    public static Integer getDeviceStatus(String deviceimei) {
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 响应对象
        CloseableHttpResponse response = null;
        try {
            // 创建httpGet
           System.out.println("获取状态："+GetConfig.getBaseurl() + DEVICE_STATUS_URL + "?deviceNo=" + deviceNo);
            HttpGet httpGet = new HttpGet(GetConfig.getBaseurl() + DEVICE_STATUS_URL + "?deviceNo=" + deviceNo);
            System.out.println(GetConfig.getBaseurl() + DEVICE_STATUS_URL + "?deviceNo=" + deviceNo);
            response = httpclient.execute(httpGet);
            String result = EntityUtils.toString(response.getEntity());
            
    
            if (StringUtil.isNotEmpty(result)) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.get("code").equals(0)) {
                    return (Integer) JSONObject.parseObject(jsonObject.get("data").toString()).get("statusCode");
                }
            }
        } catch (UnsupportedEncodingException e) {
            logger.error("获取设备状态失败"+GetConfig.getBaseurl(), e.getMessage());
        } catch (IOException e) {
            logger.error("获取设备状态失败"+GetConfig.getBaseurl(), e.getMessage());
        } catch (Exception e) {
            logger.error("获取设备状态失败"+GetConfig.getBaseurl(), e.getMessage());
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return 1;
    }
    */

	/**
	 * 0 在线 1 不在线
	 * @param deviceImei
	 * @return
	 */
    public static Integer getDeviceStatus(String deviceImei){
   
    	if(CacheReBackMsg.getDeviceStatus(deviceImei)==null){
        		boolean b=	MQClientUtil.queryDeviceStatus(deviceImei);
        		
        		if(b){
				 
				 
							CacheReBackMsg.setDeviceStatus(deviceImei, "0");
							return 0;
					 
        		}
			CacheReBackMsg.setDeviceStatus(deviceImei, "1");
			return 1;
    	}else{
    		return Integer.parseInt( CacheReBackMsg.getDeviceStatus(deviceImei).toString());
    	}
	
    	 
    }

    public static void main(String[] args) {
		System.out.println("z21".charAt(0));
		System.out.println(  getDeviceStatus("864388047215342"));
		System.out.println(  getDeviceStatus("864388047215342"));
		System.out.println(  getDeviceStatus("864388047215342"));
	}
}
